home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / sbar.cpp < prev    next >
C/C++ Source or Header  |  1994-10-10  |  7KB  |  250 lines

  1. #include "sbar.h"
  2. #include <dos.h>
  3.  
  4. ScrollBar::ScrollBar(loc p, ORIENT type, int l,
  5.     int tot, int pg_size, int cur, int pat)
  6.     {
  7.     act = 0;
  8.     pos = p;
  9.     page_size = pg_size;
  10.     direction = type;
  11.     pattern = pat;
  12.     len = l;
  13.     current = cur;
  14. //    if(tot < len)
  15. //        tot = len;
  16.     total_len = tot;
  17.     e1 = new Element(loc(0, 0), NO_ELEMENT, BUTTON_BORDER, pattern);
  18.     e2 = new Element(loc(0, 0), NO_ELEMENT, BUTTON_BORDER, pattern);
  19.     }
  20. ////////////////////////////
  21. int ScrollBar::get_screen_pos(int meter_len)
  22.     {
  23.     int l = (int)(((long)current * 10000 / (total_len - 1))
  24.                    * (meter_len - 1 - !direction) / 10000);
  25.     return l;
  26.     }
  27. ////////////////////////////
  28. void ScrollBar::meter_show(loc m, int meter_len, ORIENT direction,
  29.     int p, int pattern)
  30.     {
  31.     Meter meter;
  32.     meter.show(m, meter_len, direction, p, pattern, NO_BORDER);
  33.     }
  34. ////////////////////////////
  35. void ScrollBar::show()
  36.     {
  37.     int meter_len;
  38.     loc m;
  39.     if(direction == HORIZ)                        // Horizontal
  40.         {
  41.         if(len < 6)
  42.             return;
  43.         e1->set_rect(rect(pos, pos + loc(2, 1)));
  44.         e2->set_rect(rect(pos.X + len - 2, pos.Y, pos.X + len, pos.Y + 1));
  45.         m = loc(pos.X + 2, pos.Y);
  46.         meter_len = len - 4;
  47.         e1->set_type(LEFT_ELEMENT);
  48.         e2->set_type(RIGHT_ELEMENT);
  49.         }
  50.     else             // Vertical
  51.         {
  52.         if(len < 3)
  53.             return;
  54.         e1->set_rect(rect(pos, pos + loc(2, 1)));
  55.         e2->set_rect(rect(pos.X, pos.Y + len - 1, pos.X + 2, pos.Y + len));
  56.         m = loc(pos.X, pos.Y + 1);
  57.         meter_len = len - 2;
  58.         e1->set_type(UP_ELEMENT);
  59.         e2->set_type(DN_ELEMENT);
  60.         }
  61.     int p = get_screen_pos(meter_len);
  62.  
  63.     meter_show(m, meter_len, direction, p, pattern);
  64.  
  65. //    if(!act)
  66.         {
  67.     e1->show();
  68.         e2->show();
  69.         }
  70.     act = 1;
  71.     }
  72. ////////////////////////////
  73. rect ScrollBar::bound()
  74.     {
  75.     loc corner = !direction ? loc(pos.X + len, pos.Y + 1)
  76.                            : loc(pos.X + 2, pos.Y + len);
  77.     return screenRect(rect(pos, corner));
  78.     }
  79. ////////////////////////////
  80. void ScrollBar::repose(rect new_coord)
  81.     {
  82.     act = 0;
  83.     pos = new_coord.origin;
  84.     if(direction == HORIZ)
  85.         len = new_coord.width();
  86.     else
  87.         len = new_coord.height();
  88.     }
  89. ////////////////////////////
  90. int ScrollBar::mouse_in(loc where)
  91.     {
  92.     if(e1->mouse_in(where))
  93.         return 1;
  94.     if(e2->mouse_in(where))
  95.         return 2;
  96.     if(bound().contains(where))
  97.         {
  98.         int meter_len;
  99.         if(direction == HORIZ)
  100.             meter_len = len - 4;
  101.         else
  102.             meter_len = len - 2;
  103.         int p = get_screen_pos(meter_len) + (!direction ? pos.X : pos.Y);
  104.         if(direction == HORIZ)
  105.             {
  106.             p = screenXL(p);
  107.             if(p < where.X)
  108.                 return 4;
  109.             else
  110.                 return 3;
  111.             }
  112.         p = screenYT(p);
  113.         if(p < where.Y)
  114.             return 4;
  115.         else
  116.             return 3;
  117.  
  118.         }
  119.     return 0;
  120.     }
  121. ////////////////////////////
  122. void ScrollBar::decrease()
  123.     {
  124.     global_i[0] = direction == HORIZ ? AC_HOME : AC_PG_UP;
  125.     current = current - page_size > 0 ? current - page_size
  126.                                       : 0;
  127.     ::delay(200);
  128.     show();
  129.     }
  130. /////////////////////////
  131. void ScrollBar::increase()
  132.     {
  133.     global_i[0] = direction == HORIZ ? AC_END : AC_PG_DN;
  134.     current = current + page_size < total_len
  135.                 ? current + page_size : total_len;
  136.     ::delay(200);
  137.     show();
  138.     }
  139. ////////////////////////////
  140. void ScrollBar::exe(int)
  141.     {
  142.     if(!act)
  143.         return;
  144.     while(1)
  145.     {
  146.     mouseShowCursor();
  147.     get_event();       // Defined in ADDEVENT.H, ADDEVENT.CPP
  148.     mouseHideCursor();
  149.     if(e.what == MOUSEEVENT)    // it was mouse
  150.         {
  151.             switch(mouse_in(e.where()))
  152.                 {
  153.                 case 1:             // e1 pressed.
  154.             e1->press();
  155.             ::delay(200);
  156.             e1->release();
  157.                     e1->unhilite();
  158.             global_i[0] = direction == HORIZ ? AC_LEFT : AC_UP;
  159.                     current = current > 0 ? current - 1 : 0;
  160.                     show();
  161.                     break;
  162.                 case 2:             // e2 pressed.
  163.             e2->press();
  164.             ::delay(200);
  165.             e2->release();
  166.                     e2->unhilite();
  167.             global_i[0] = direction == HORIZ ? AC_RIGHT : AC_DOWN;
  168.                     current = current < total_len ? current + 1 : total_len;
  169.                     show();
  170.             break;
  171.                 case 3:
  172.                     decrease();
  173.                     global_i[0] = action_type;
  174.                     global_num = current + 1;
  175.                     return;
  176.                 case 4:
  177.                     increase();
  178.                     global_i[0] = action_type;
  179.                     global_num = current + 1;
  180.                     return;
  181.                 default:             // Outside.
  182.                     global_i[0] = 0;
  183.                     break;
  184.                 }
  185.         global_num = current;
  186.         return;
  187.         }
  188.     else
  189.         {
  190.         switch(e.key)
  191.         {
  192.                 case EVENT_F10:
  193.             mouseShowCursor();
  194.             global_i[0] = 0;
  195.                     global_num = current + 1;
  196.             return;
  197.         case EVENT_F1: return;
  198.         case EVENT_ESC:
  199.         case EVENT_TAB:
  200.         case EVENT_F6:
  201.         case EVENT_ALT_F3:
  202.         case EVENT_ALT_F4:
  203.         case EVENT_ALT_TAB:
  204.                 case EVENT_RETURN:
  205.             mouseShowCursor();
  206.             global_i[0] = action_type;
  207.                     global_num = current + 1;
  208.             return;
  209.                 case EVENT_LEFT:
  210.                 case EVENT_UP:
  211.                     global_i[0] = direction == HORIZ ? AC_LEFT : AC_UP;
  212.                     current = current > 0 ? current - 1 : 0;
  213.                     show();
  214.                     break;
  215.                 case EVENT_RIGHT:
  216.                 case EVENT_DN:
  217.             global_i[0] = direction == HORIZ ? AC_RIGHT : AC_DOWN;
  218.                     current = current < total_len ? current + 1 : total_len;
  219.                     show();
  220.                     break;
  221.         case EVENT_F2:
  222.         default:
  223.             break;                   // return;
  224.         }
  225.             global_num = current;
  226.         }
  227.      }
  228.  
  229.     }
  230. ////////////////////////////
  231. /*
  232. void main()
  233.     {
  234.     if(!init_KNOW_HOW())
  235.         return;
  236.     setfillstyle(SOLID_FILL, pColorSet->colors.BAK_COLOR);
  237.     bar(0, 0, getmaxx(), getmaxy());
  238.  
  239.     ScrollBar s(loc(10, 10), HORIZ, 40, 100, 10, 50, 16);
  240.     s.show();
  241.     ScrollBar s1(loc(60, 5), VERT, 15, 100, 10, 40, 16);
  242.     s1.show();
  243.  
  244.     s.exe();
  245.     s1.exe();
  246.  
  247.     close_KNOW_HOW();
  248.     closegraph();
  249.     }
  250. */